package com.gitee.wsl.doc.prosemirror.transform.ext

import com.gitee.wsl.doc.prosemirror.model.fragment.Fragment
import com.gitee.wsl.doc.prosemirror.model.fragment.Slice
import com.gitee.wsl.doc.prosemirror.model.node.Node
import com.gitee.wsl.doc.prosemirror.transform.TransformAble

// Replace the part of the document between `from` and `to` with the given `slice`.
fun<T: TransformAble> T.replace(from: Int, to: Int = from, slice: Slice = Slice.empty) = this.apply{
    val step = this.doc.replaceStep( from, to, slice)
    if (step != null) this.step(step)
    //return this
}

// Replace the given range with the given content, which may be a fragment, node, or array of nodes.
fun<T: TransformAble> T.replaceWith(from: Int, to: Int, content: Fragment)
        =this.replace(from, to, Slice(Fragment.from(content), 0, 0))


fun<T: TransformAble> T.replaceWith(from: Int, to: Int, content: Node)
        =this.replace(from, to, Slice(Fragment.from(content), 0, 0))


fun<T: TransformAble> T.replaceWith(from: Int, to: Int, content: List<Node>)
        = this.replace(from, to, Slice(Fragment.from(content), 0, 0))



